package src.mcpichess.uci;

/** This class simply holds four numbers: startX, startY, endX, and endY **/
public class Move {
	
	public int startX, startY, endX, endY;
	public String moveString = "";
	
	public static final String[] LetterArray = {"h","g","f","e","d","c","b","a"};
	
	
	/** Public constructor: Provide all four members */
	public Move(int sX, int sY, int eX, int eY)
	{
		this.startX = sX;
		this.startY = sY;
		this.endX = eX;
		this.endY = eY;
		
		this.moveString = numberToLetter(startX,startY,endX,endY);
	}
	
	
	/** Public constructor: Provide an algebraic string **/
	public Move(String s)
	{
		this.moveString = s;
		
		this.startX = letterToNumber(s).startX;
		this.startY = letterToNumber(s).startY;
		this.endX = letterToNumber(s).endX;
		this.endY = letterToNumber(s).endY;
	}
	
	
	/** Convert coordinate notation to algebraic notation **/
	public static String numberToLetter(int startX, int startY, int endX, int endY)
	{
		String s = LetterArray[startX] + String.valueOf(startY+1) + LetterArray[endX] + String.valueOf(endY+1);
		return s;
	}
	
	/** Easier to use version of original **/
	public static String numberToLetter(Move move)
	{
		return numberToLetter(move.startX, move.startY, move.endX, move.endY);
	}
	
	/** Convert algebraic notation to coordinate location, as a Move **/
	public static Move letterToNumber(String s)
	{
		if(s.length() != 4) //Length check
		{
			return null;
		}
		
		Move m = new Move(0,0,0,0);
		
		m.startX = indexOf(String.valueOf(s.charAt(0)));
		m.startY = Integer.parseInt(String.valueOf(s.charAt(1))) - 1;
		m.endX = indexOf(String.valueOf(s.charAt(2)));
		m.endY = Integer.parseInt(String.valueOf(s.charAt(3))) - 1;
		m.moveString = s;
		
		return m;
	}
	
	
	/** Return the index (From LetterArray) of the given argument **/
	public static int indexOf(String target)
	{
		for(int i = 0; i < LetterArray.length; i++)
		{
			if(LetterArray[i].equals(target))
				return i;
		}
		
		return -1;
	}
	
	

}
